package modulesAPI.v2.composite;

import java.util.ArrayList;

import person.Guest;

import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCheckBox;
import modulesAPI.v2.basics.ModCollectionViewer;
import modulesAPI.v2.basics.ModIntTextField;
import warehouse.Table;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

public class MTable extends Module<Table>{
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un tavolo
	 * */
	public MTable(Module<?> ownerModule, Table content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		this.createModules();
		this.addModules(number, seatNumber, disponibility, guests);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un tavolo
	 * */
	private void createModules(){
		this.seatNumber = new ModIntTextField(this, this.getContent().getSeatNumber(), "SEAT_NUMBER", "SEAT_NUMBER_DESC", this.getViewType());
		this.number = new ModIntTextField(this, this.getContent().getTableNumber(), "TABLE_NUMBER", "TABLE_NUMBER_DESC", this.getViewType());
		this.disponibility =new ModCheckBox(this, this.getContent().getDisponibility(), "DISPONIBILITY", "DISPONIBILITY_DESC", this.getViewType());
		ArrayList<Module<?>> arr = new ArrayList<Module<?>>();
		for(Guest g: this.getContent().getGuests())
			arr.add(new MGuest(this, g, "GUEST", "GUEST_DESC", this.getViewType()));
		guests = new ModCollectionViewer(this, arr, "TABLE_GUESTS", "TABLE_GUESTS_DESC", this.getViewType());
		guests.addModuleChangedListener(this);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getTableNumber()+"  "+CWords.getWord("SEAT_NUMBER")+": "+this.getContent().getSeatNumber(), this.getContent().getDisponibility()?CWords.getWord("DISPONIBLE"):CWords.getWord("NOT_DISPONIBLE"), isSelected);
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	protected void beforeFireChange(){
		this.setContent(new Table(
				this.getContent().getId(),
				disponibility.getContent(),
				number.getContent(),
				seatNumber.getContent()
				));
	}
	@Override
	protected void beforeFireChange(Module<?> mod) {		
	}
	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	private ModCheckBox disponibility;
	private ModIntTextField seatNumber;
	private ModIntTextField number;
	private ModCollectionViewer guests;
}
